package com.eatme.leetcode.problems;

import java.util.Arrays;

/**
 *  875. 爱吃香蕉的珂珂 koko-eating-bananas
 */
public class Solution875 {

    public int minEatingSpeed(int[] piles, int h) {
        int l =1, r= Arrays.stream(piles).max().getAsInt();
        while (l<r) {
            int mid = l+(r-l)/2;
            if (eatingTime(piles,mid) <= h)
                r=mid;
            else
                l =mid+1;
        }
        return l;
    }

    private int eatingTime(int[] piles, int k) {
        int res = 0;
        for (int pile: piles)
            res += pile / k + ( pile % k > 0 ? 1 : 0);
        return res;
    }

    public static void main(String[] args) {
        int[] piles = {30,11,23,4,20};
        int h = 7;
        System.out.println(new Solution875().minEatingSpeed(piles,h));
    }
}